package com.qifan.energy.activity;

import android.os.Bundle;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.blankj.utilcode.util.StringUtils;
import com.qifan.energy.MainActivity;
import com.qifan.energy.base.BaseActivity;
import com.qifan.energy.bean.result.LoginResult;
import com.qifan.energy.bean.vo.LoginVO;
import com.qifan.energy.R;
import com.qifan.energy.global.Keys;
import com.qifan.energy.http.net.NetUtils;
import com.qifan.energy.http.response.MyObserver;
import com.qifan.energy.util.ActivityStackUtil;
import com.qifan.energy.util.IntentUtils;
import com.qifan.energy.util.SharedPreferencesUtil;
import com.qifan.energy.util.ToastUtils;
import com.qifan.energy.widget.CustomTitleBar;
import com.qifan.energy.widget.dialog.LoadingUtils;
import com.orhanobut.logger.Logger;

import butterknife.BindView;
import butterknife.OnClick;

public class SignInActivity extends BaseActivity {

    /*@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_sign_in);
    }*/

    @BindView(R.id.title_layout_bar)
    CustomTitleBar titleBar;
    @BindView(R.id.login_btn)
    TextView loginBtn;

    @BindView(R.id.phone_et)
    EditText phoneEt;
    @BindView(R.id.pwd_et)
    EditText pwdEt;

    private boolean isPwdVisible = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sign_in;
    }

    @Override
    protected void initView(Bundle savedInstanceState) {
        /* 更改状态栏 */
        changeStatusTransparentFontBlack();

        titleBar.setRightBtnClickListener((v)-> {
            IntentUtils.startActivity(SignInActivity.this,SignUpActivity.class);
        });
    }

    @Override
    protected void initData() {

    }

    @OnClick(R.id.login_btn)
    void onLoginBtnClicked() {
        String phone = phoneEt.getText().toString();
        String pwd = pwdEt.getText().toString();
        if (TextUtils.isEmpty(phone)){
            ToastUtils.showShort("手机号不能为空");
            return;
        }
        if (TextUtils.isEmpty(pwd)){
            ToastUtils.showShort("密码不能为空");
            return;
        }

        //更改按钮状态
        loginBtn.setEnabled(false);
        LoadingUtils.show(this);
        LoginVO loginVO = new LoginVO();
        loginVO.setUsername(phone);
        loginVO.setPassword(pwd);
        NetUtils.login(loginVO, new MyObserver<LoginResult>(this) {
            @Override
            public void onSuccess(LoginResult result) {
                loginBtn.setEnabled(true);
                LoadingUtils.hide();

                //保存token
                if (!StringUtils.isEmpty(result.getToken())){
                    Logger.i("token->authorization->:",result.getToken());
                    NetUtils.saveToken(result.getToken());
                    SharedPreferencesUtil.putData(Keys.TOKEN_KEY,result.getToken());
                }

                //提示并跳转页面
                ToastUtils.showShort("登录成功!");
                ActivityStackUtil.finishActivity();
                IntentUtils.startActivity(SignInActivity.this, MainActivity.class);
            }

            @Override
            public void onFailure(Throwable e, String errorMsg) {
                loginBtn.setEnabled(true);
                LoadingUtils.hide();
                ToastUtils.showShort(errorMsg);

                //TODO：测试代码
                /*ActivityStackUtil.finishActivity();
                IntentUtils.startActivity(SignInActivity.this, MainActivity.class);*/
            }
        });
    }

    @OnClick(R.id.forget_tv)
    void onForgetTvClicked(){
        IntentUtils.startActivity(SignInActivity.this, ForgetPwdActivity.class);
    }

    @OnClick(R.id.password_eye_iv)
    void onPasswordEyeIvClicked(ImageView imageView){
        isPwdVisible = !isPwdVisible;
        if (isPwdVisible){
            imageView.setImageResource(R.mipmap.ic_edit_eye_open_gray);
            pwdEt.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            imageView.setImageResource(R.mipmap.ic_edit_eye_close_gray);
            pwdEt.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        pwdEt.setSelection(pwdEt.getText().length());
    }
}
